<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Captcha {

    protected $CI;
    protected $val1;
    protected $val2;
    protected $operador;
    protected $total;    

    function Captcha() {
        $this->CI = & get_instance();
    }

    public function render($titulo = ""){
        switch(rand(0,1)){
            case 0:
                $this->_suma();
                $this->operador = " + ";
                break;
            case 1:
                $this->_resta();
                $this->operador = " - ";
                break;
        }
        $cadena  = '<label>'.$titulo.' <span>*</span></label>';
        $cadena .= '<span class="font-size17">'.$this->val1.$this->operador.$this->val2." = ".'<input type="text" class="input-small" style="margin-bottom: 0px" name="p_captcha" id="p_captcha"/></span>';
        $cadena .= '<input type="hidden" name="p_total" id="p_total" value="'.md5($this->total).'"/>';
        return $cadena;
    }

    private function _suma(){
        $this->val1 = rand(1, 5);
        $this->val2 = rand(1, 5);
        $this->total = $this->val1 + $this->val2;
    }

    private function _resta(){
        $this->val1 = rand(1, 5);
        $this->val2 = rand(1, $this->val1);
        $this->total = $this->val1 - $this->val2;
    }
}

?>